﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using EngineOfEvermore.Common;

namespace EngineOfEvermore.Scene.Visual
{
    public sealed class VisualComponent : SceneObjectComponent
    {
        #region UpdateTask class
        class UpdateTask : Task
        {
            public UpdateTask( VisualComponent owner )
                : base( 1000 )
            {
                _owner = owner;
            }

            public override void Execute( LogicUpdateEventArgs e )
            {
                _owner._update( e );
            }

            VisualComponent _owner;
        }
        #endregion

        #region ChildCollection class
        public sealed class ChildCollection : NamedObjectCollection<VisualComponentChild>
        {
            VisualComponent _owner;

            internal ChildCollection( VisualComponent owner )
            {
                _owner = owner;
            }

            protected override void _onItemAdded( VisualComponentChild item )
            {
                item.OnAddToVisualComponent( _owner );

                foreach ( VisualComponentChild sibling in this )
                {
                    if ( sibling != item )
                    {
                        sibling.OnSiblingAdded( item );
                        item.OnSiblingAdded( sibling );
                    }
                }

                if ( _owner.Scene != null )
                {
                    VisualSubSystem vss = _owner.Scene.GetSubSystem<VisualSubSystem>();

                    vss.RegisterDrawable( item );
                }
            }

            protected override void _onItemRemoved( VisualComponentChild item )
            {
                if ( _owner.Scene != null )
                {
                    VisualSubSystem vss = _owner.Scene.GetSubSystem<VisualSubSystem>();

                    vss.UnregisterDrawable( item );
                }

                foreach ( VisualComponentChild sibling in this )
                {
                    if ( sibling != item )
                    {
                        item.OnSiblingRemoved( sibling );
                        sibling.OnSiblingRemoved( item );
                    }
                }

                item.OnRemoveFromVisualComponent( _owner );
            }
        }
        #endregion

        #region Variables
        Task _updateTask;
        ChildCollection _children;
        Vector2d _positionOffset;
        float _rotationOffset;
        #endregion

        #region Constructor
        public VisualComponent()
        {
            _updateTask = new UpdateTask( this );
            _children = new ChildCollection( this );
        }
        #endregion

        #region Properties
        public Vector2d PositionOffset
        {
            get { return _positionOffset; }
            set { _positionOffset = value; }
        }

        public float RotationOffset
        {
            get { return _rotationOffset; }
            set { _rotationOffset = value; }
        }

        public ChildCollection Children
        {
            get { return _children; }
        }
        #endregion

        #region EntityComponent Callbacks
        internal protected override void OnAddToScene( SceneGraph scene )
        {
            base.OnAddToScene( scene );

            VisualSubSystem vss = scene.GetSubSystem<VisualSubSystem>();

            foreach ( VisualComponentChild child in _children )
            {
                vss.RegisterDrawable( child );
            }

            scene.TimeContext.AddRepeatingTask( _updateTask );
        }

        internal protected override void OnRemoveFromScene( SceneGraph scene )
        {
            scene.TimeContext.RemoveRepeatingTask( _updateTask );

            VisualSubSystem vss = scene.GetSubSystem<VisualSubSystem>();

            foreach ( VisualComponentChild child in _children )
            {
                vss.UnregisterDrawable( child );
            }

            base.OnRemoveFromScene( scene );
        }
        #endregion
        
        #region Private Methods
        void _update( LogicUpdateEventArgs e )
        {
            foreach ( VisualComponentChild child in _children )
            {
                if ( child.NeedsUpdate )
                {
                    child.Update( e );
                }
            }
        }
        #endregion
    }
}
